package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/v1/file")
@Tag(name = "上传接口")

public class UpLoadController {
    @Operation(summary = "上传接口")
    @PostMapping("/upload")
    public JsonResult upload(MultipartFile  file) throws IOException {
        String filename = file.getOriginalFilename();
        log.debug("开始处理【上传】的请求，参数：{}", filename);
        String filename1 = filename.substring(filename.lastIndexOf("."));
        log.debug("文件后缀名：{}", filename1);
        UUID uuid = UUID.randomUUID();
        log.debug("随机数：{}", uuid);

        String newFilename = uuid + filename1;
        log.debug("新文件名：{}", newFilename);
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String path = "d:/Files"+sdf.format(new Date());
        log.debug("保存路径：{}", path);
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        file.transferTo(new File(path + newFilename));
        return JsonResult.ok(sdf.format(new Date())+newFilename);
    }
}
